<?php
namespace Portal\Controller;
use Common\Controller\HomebaseController;

/**
 * 首页
 */
class ShopController extends HomebaseController {

    protected $shop_model;
    protected $shop_size;
    protected $shopcat_model;
    protected $study_model;
    protected $slide_model;

    function _initialize() {
        parent::check_login();
        parent::_initialize();
        $this->shop_model = D("Common/Shop");
        $this->shopcat_model = D("Common/ShopCat");
        $this->study_model = D("Common/Study");
        $this->slide_model = D("Common/Slide");
        $this->shop_size = D("Common/ShopSize");
    }

	public function index() {
        $cat = I('cat');
        if($cat){
            $where['shop_cat_id'] = $cat;
        }
        $sort = I('sort');
        $sort_type = I('sort_type','desc');
        if($sort && $sort_type){
            $order = $sort.' '.$sort_type;
            if($sort == 'shop_price'){
                $cls1 = 'double';
                if($sort_type == 'asc'){
                    $cls2 = 'down';
                    $sort_type = 'desc';
                }else{
                    $cls2 = 'up';
                    $sort_type = 'asc';
                }
            }else{
                $cls2 = 'double';
                if($sort_type == 'asc'){
                    $cls1 = 'down';
                    $sort_type = 'desc';
                }else{
                    $cls1 = 'up';
                    $sort_type = 'asc';
                }
            }
        }else{
            $order = "shop_price desc";
            $cls2 = 'double';
            $cls1 = 'down';
        }
        $where['shop_sale'] = 1;
        $where['shop_status'] = 1;
        $shops = $this->shop_model->where($where)->order($order)->select();
        foreach ($shops as $k => $v){
            $shops[$k]['smeta'] = json_decode($v['smeta'],true);
        }

        //获取商品分类
        //一级
        $cats = $this->shopcat_model->where(array('cat_status'=>1,'parentid'=>0))->select();
        foreach ($cats as $k => $v) {
            $cats[$k]['f'] = $this->shopcat_model->where(array('cat_status'=>1,'parentid'=>$v['cid']))->select();
        }
        $shop_num = M('cart')->where(array('user_id'=>session('user')['id']))->sum('shop_num');
        $this->assign('cats',$cats);
        $this->assign('cls1',$cls1);
        $this->assign('cls2',$cls2);
        $this->assign('sort_type',$sort_type);
        $this->assign('shops',$shops);
        $this->assign('shop_num',$shop_num);
        $this->display(':sale');
    }

    //商品详情页
    public function detail(){
        $id = I('shop_id',0);
        if(!$id){
            $this->error('非法请求');
        }
        $shop = $this->shop_model->where(array('id'=>$id))->find();
        $size = json_decode($shop['shop_size'],true);
        $shop['shop_size'] = $size;
        $shop['smeta'] = json_decode($shop['smeta'],true);//解密图片地址
        $shop['imgs'][] = $shop['smeta']['thumb'];//组成新的轮播图数组
        foreach ($shop['smeta']['photo'] as $v){
            $shop['imgs'][] = $v['url'];
            unset($shop['smeta']);
        }
        $cart_num = M('cart')->where(array('user_id'=>session('user')['id']))->sum('shop_num');
        $this->assign('shop',$shop);
        $this->assign('cart_num',$cart_num);
        $this->display(':detail');
    }

    //购物车页
    public function cart(){
        if(IS_POST){//购物车添加
            $data['shop_id'] = I('shop_id');
            $data['shop_num'] = I('shop_num');
            $data['size_name'] = I('spec_name');
            $data['size_id'] = I('spec_key');
            $data['user_id'] = session('user')['id'];
            $m = M('cart');

            $carts = M('cart')->select();
            foreach ($carts as $k => $v){
                if($v['shop_id'] == $data['shop_id'] && $data['size_id'] == $v['size_id']){
                    $re = M('cart')->where(array('cartid'=>$v['cartid']))->save(array('shop_num'=>$v['shop_num'] + $data['shop_num']));
                }
            }
            if(!$re){
                $re = $m->add($data);
            }
            if($re){
                $arr = array('status'=>1);
            }else{
                $arr = array('status'=>2,'msg'=>"购物车添加失败");
            }



            /*$m->startTrans();//开启事务
            //购物车添加
            $re = $m->add($data);
            //库存减少
            $shop = M('shop')->where(array('id'=>$data['shop_id']))->find();
            $shop['shop_size'] = json_decode($shop['shop_size'],true);
            foreach ($shop['shop_size'] as $k => $v){
                if($v['size_id'] == $data['size_id']){
                    $shop['shop_size'][$k]['kc'] = $v['kc'] - $data['shop_num'];
                }
            }
            $save['shop_size'] = json_encode($shop['shop_size']);
            $save['shop_sales'] = $shop['shop_sales'] + 1;
            $re1 = M('shop')->where(array('id'=>$data['shop_id']))->save($save);
            if($re && $re1){
                $m->commit();
                $arr = array('status'=>1);
            }else{
                $m->rollback();
                $arr = array('status'=>2,'msg'=>"购物车添加失败");
            }*/

            echo json_encode($arr);
        }else{
            $carts = M('cart as c')->join('think_shop as s on s.id = c.shop_id')->field('c.*,s.*')->where(array('c.user_id'=>session('user')['id']))->select();
            foreach ($carts as $k => $v){
                $carts[$k]['smeta'] = json_decode($v['smeta'],true);
            }
            $this->assign('carts',$carts);
            $this->display(':cart');
        }
    }

    //购物车删除
    public function cartdel(){
        $cartid = I('cartid');
        if(!$cartid){
            $data['status'] = 2;
            $data['msg'] = '非法请求';
        }
        $re = M('cart')->where(array('cartid'=>$cartid))->delete();
        if($re){
            $data['status'] = 1;
        }else{
            $data['status'] = 3;
            $data['msg'] = '删除失败';
        }
        echo json_encode($data);
    }

    //购物车加减
    public function cartnum(){
        $cartid = I('cartid');
        $num = I('num');
        M('cart')->where(array('cartid'=>$cartid))->save(array('shop_num'=>$num));
    }

    //提交订单页
    public function sorder(){
        $cart_id = I('cart_id');//接收提交的购物车商品id
        //查询提交订单信息

        $this->addr = M('addr')->where(array('check'=>1))->find();

        $carts = M('cart as c')->join('think_shop as s on s.id = c.shop_id')->field('c.*,s.shop_title,s.shop_pany')->where(array('c.cartid'=>array('in',$cart_id)))->select();
        $this->assign('carts',$carts);

        $this->display(':sorder');
    }
}